/**
 * Created by Administrator on 2017/1/5.
 */

var pageSize = 8;//每页条数
var pageStart = 1;//当前页码
var pageCount = 10;//总页数
var type = 0;//查询类型：0 1 2 3 4 5 6 7
var para = "";//查询条件0
window.onload = function () {
    $(".pagination a").eq(1).css("background-color", "#eee");
    setClass(type,para);
	showTeachers();
    showinstitute();
};

function sureBtn() {
	if($("#myModalLabel").text()=="班级添加"){
		addclass();
	}else if($("#myModalLabel").text()=="班级查看"){
		hideModel();
	}else{
		alert("班级编辑");
	}
}

//初始化班级信息
var setClass= function (type,para) {
	var url="/huaqiao/tclass/getByPage?type=" + type + "&para=" + para + "&pageStart=" + pageStart + "&pageSize=" + pageSize + "&time=" + new Date().getTime();
	 $.getJSON(url, function (mgs) {
	        if (mgs == null || mgs.length == 0) {
	            alert("找不到数据");
	            return;
	        }
	        var str="";
	        for (var i = 0; i < mgs.length; i++) {
	        	var modiDate=new Date(mgs[i].modifiedTime);
	            str += "<tr>";
	            str += "<td><input type='checkbox' name='pid' id='class" + i + "' value='" + mgs[i].id + "'></td>";
	            str += "<td>" + mgs[i].institutename + "</td>" +
	                "<td>" + mgs[i].majorname+ "</td>" +
	                "<td>" + mgs[i].grade + "</td>" +
	                "<td>" + mgs[i].code + "</td>" +
	                "<td>" + mgs[i].name + "</td>" +
	                "<td>" + modiDate.getFullYear()+"-"+setFullDate(modiDate.getMonth()+1)
	                +"-"+setFullDate(modiDate.getDay())+"&nbsp;"+setFullDate(modiDate.getHours())+":"+setFullDate(modiDate.getMinutes())
	                +":"+setFullDate(modiDate.getSeconds()) + "</td>" +
	                "<td>" + mgs[i].teachername + "</td>";
	            str += "<td><a href='#' onclick='showStuInfo("+ mgs[i].code+")'>查看</a>/<a href='#' onclick='editStuInfo("+ mgs[i].code+")'>编辑</a></td>";
	            str += "</tr>";
	        }
	        $("#class_body").html(str);
		    
		    
		    url = "/huaqiao/tclass/getByPage?type=" + type + "&para=" + para + "&time=" + new Date().getTime();
		    $.get(url, function (res) {
		        pageCount = Math.floor((res + pageSize - 1) / pageSize);
		        $("#pageCount").html(pageCount);
		        setTableBTN();
		    });
	});
}



//将timestamp格式转化为 YYYY-MM-DD HH-mm-SS
   function setFullDate(num){
	   num+="";
	   if(num.length<2){
		   num="0"+num;
	   }
	   return num;
   }

 //设置翻页区域按钮
   var setTableBTN = function () {
       $(".pagination li").each(function (i) {
           if (i == 0) {//上一页按钮
               this.onclick = function () {
                   if (pageStart > 1) {
                       pageStart--;

                       if (pageStart < parseInt($(".pagination a").eq(1).html())) {
                           $(".pagination a").eq(5).html(pageStart + 4);
                           $(".pagination a").eq(4).html(pageStart + 3);
                           $(".pagination a").eq(3).html(pageStart + 2);
                           $(".pagination a").eq(2).html(pageStart + 1);
                           $(".pagination a").eq(1).html(pageStart);
                       }
                       $(".pagination a").each(function (i) {
                           $(".pagination a").eq(i).css("background-color", "#fff");
                           if (pageStart == parseInt($(".pagination a").eq(i).html())) {
                               $(".pagination a").eq(i).css("background-color", "#eee");
                           }
                       });

                       setClass(type,para);
                   } else {
                       alert("当前是第一页");
                   }
               };
           } else if (i == 6) {//下一页按钮
               this.onclick = function () {
                   if (pageStart < pageCount) {
                       pageStart++;

                       if (pageStart > parseInt($(".pagination a").eq(5).html())) {
                           $(".pagination a").eq(5).html(pageStart);
                           $(".pagination a").eq(4).html(pageStart - 1);
                           $(".pagination a").eq(3).html(pageStart - 2);
                           $(".pagination a").eq(2).html(pageStart - 3);
                           $(".pagination a").eq(1).html(pageStart - 4);
                       }
                       $(".pagination a").each(function (i) {
                           $(".pagination a").eq(i).css("background-color", "#fff");
                           if (pageStart == parseInt($(".pagination a").eq(i).html())) {
                               $(".pagination a").eq(i).css("background-color", "#eee");
                           }
                       });
                       setClass(type,para);
                   } else {

                       alert("当前是最后一页");
                   }
               };
           } else {//普通翻页按钮
               this.onclick = function () {
                   var index = parseInt(this.innerText);
                   if (index > pageCount) {
                       index = pageCount;
                   }
                   pageStart = index;
                   $(".pagination a").each(function (i) {
                       $(".pagination a").eq(i).css("background-color", "#fff");

                   });
                   $(".pagination a").eq(index).css("background-color", "#eee");
                   // pageStart = parseInt(this.);
                   setClass(type,para);
               }
           }
       });
   };

   
   
   
   
   
//输入班级名称或编号进行模糊查询
   function criteria(){
			if ($("#search").val() != "") {
		        if ($.isNumeric($("#search").val())) {//按班级编号模糊
		            type = 1;
		            para = $("#search").val();
		            setClass(type,para);
		        } else {//按班级名称
		            type = 2;
		            para = $("#search").val();
		            setClass(type,para);
		        }
		    } else {
		        alert("条件不能为空");
		    }
		
	   
		/*var name =$('#search').val();
		if(name==""){
			return alert("请输入查询条件！");
		}
		$.ajax({
			type:"post",
			url:"/huaqiao/tclass/blurfind",
			data:{
				name:name
			},
			success:function(mgs){
				var mgs1=JSON.stringify(mgs);
				// alert(mgs1);
				 var str = "";
 		        for (var i = 0; i < mgs.length; i++) {
 		        	var modiDate=new Date(mgs[i].modifiedTime);
 		            str += "<tr>";
 		            str += "<td><input type='checkbox' name='classes' id='class" + i + "' value='" + mgs[i].id + "'></td>";
 		            str += "<td>" + mgs[i].institutename + "</td>" +
 		                "<td>" + mgs[i].majorname+ "</td>" +
 		                "<td>" + mgs[i].grade + "</td>" +
 		                "<td>" + mgs[i].code + "</td>" +
 		                "<td>" + mgs[i].name + "</td>" +
 		                "<td>" + modiDate.getFullYear()+"-"+setFullDate(modiDate.getMonth()+1)
 		                +"-"+setFullDate(modiDate.getDay())+"&nbsp;"+setFullDate(modiDate.getHours())+":"+setFullDate(modiDate.getMinutes())
 		                +":"+setFullDate(modiDate.getSeconds()) + "</td>" +
 		                "<td>" + mgs[i].teachername + "</td>";
 		            str += "<td><a href='#'>查看</a>/<a href='#'>编辑</a></td>";
 		            str += "</tr>";
 		        }
 		        $("#class_body").html(str);
			},
			error:function(){
				alert("未查找到符合条件的信息");
			}
		});*/
	}

   //添加班级信息
   function addclass(){
	   $("#myModalLabel").text("班级添加");
	  var teachername=$('#teachername').val();
	  var majorname=$('#majorname').val();
	  var institutename=$('#institutename').val();
	  var name=$('#name').val();
	  var code=$('#code').val();
	  var grade=$('#grade').val();
	  
	  if(teachername=="" && majorname=="" && institutename=="" && name=="" && code=="" && grade==""){
		  return $('#information').text("请输入相关信息");;
	  }else if(teachername=="" || majorname=="" || institutename=="" || name=="" || code=="" || grade==""){
		  return $('#information').text("请输入完整信息");;
	  }
	  if(!(/^\d{4,6}$/.test(code))){
			return $('#information').text("编号请输入4-6位的纯数字");
	  }
		$.ajax({
			type:"post",
			url:"/huaqiao/tclass/addclass",
			data:{
				teachername:teachername,
				majorname:majorname,
				institutename:institutename,
				name:name,
				code:code,
				grade:grade
			},
			beforeSend:function (xhr) { $('#myModal').modal('hide');},
			success:function(mgs){
					alert("添加成功");
					hideModel();
			},
			error:function(){
				alert("添加失败");
				hideModel();
			}
		});
	}


//选中记录进行删除
	function delectselect(){
		obj = document.getElementsByName("classes");
	    check_val = [];
	    for(k in obj){
	        if(obj[k].checked)
	            check_val.push(obj[k].value);
	    }
		if(check_val.length<1){
			hideAlert();
			return alert("请至少选择一条需删除的记录?")
		}
		$.ajax({
			type:"post",
			url:"/huaqiao/tclass/deleteclass",
			data:{
				id:check_val
			},
			traditional:true,
			success:function(mgs){
					alert("删除成功");
					hideAlert();
			},
			error:function(){
				alert("删除失败");
				hideAlert();
			}
		});
	}
//获得还未担任班主任的老师
// function getTeacher(){
// 	$.ajax({
// 		type:"post",
// 		url:"/huaqiao/tclass/getTeacher?time=" + new Date().getTime(),
// 		success:function(mgs){
// 			for(var i=0;i<mgs.length;i++){
// 				$('#head_teacher').prepend('<option>' + mgs[i] + '</option>');
// 			}
// 		},
// 		error:function(){
// 			alert("fff");
// 		}
// 	});
// }
//显示班主任
function showTeachers() {
	getAllTeachers(0,"");
}
function getAllTeachers(type,para) {
	var url="/huaqiao/tclass/getTeacher?time=" + new Date().getTime();
	$.getJSON(url, function (mgs) {
		if (mgs == null || mgs.length == 0) {
			alert("找不到数据");
			return;
		}
		// var str="";
		// for (var i = 0; i < mgs.length; i++) {
		// 	str += "<tr>";
		// 	str += "<td><input type='checkbox' name='pid' id='teacher" + i + "' value='" + mgs[i].id + "'></td>";
		// 	str += "<td>" + mgs[i].code + "</td>" +
		// 		"<td>" + mgs[i].name + "</td>" +
		// 		"<td>" + mgs[i].mobile + "</td>" +
		// 		"<td>" + mgs[i].password + "</td>" +
		// 		"<td>" + mgs[i].weixinId + "</td>"+
		// 		"<td>" + mgs[i].modifiedTime + "</td>"+
		// 		"<td>" + mgs[i].modifiedBy + "</td>";
		// 	str += "<td><a href='#'>查看</a>/<a href='#'>编辑</a></td>";
		// 	str += "</tr>";
		// }
		// $("#teacher_body").html(str);
		// url = "/huaqiao/teacher/getByPage?type=" + type + "&para=" + para + "&time=" + new Date().getTime();
		// $.get(url, function (res) {
		// 	pageCount = Math.floor((res + pageSize - 1) / pageSize);
		// 	$("#pageCount").html(pageCount);
		// 	setTableBTN();
		// });
		for(var i=0;i<mgs.length;i++){
			$('#teachername').prepend('<option>' + mgs[i] + '</option>');
		}
	});
}
//显示学院，选中学院后联动显示出学院内可选专业
function showinstitute() {
    var url = "/huaqiao/StudentBack/getOrg?time=" + new Date().getTime();
    $.getJSON(url, function (strJSON) {
		for(var i=0;i<strJSON.length;i++){
			$('#institutename').prepend('<option>' + strJSON[i].institutename + '</option>');
		}
		//
		 document.getElementById("institutename").oninput=function () {
			 $('#majorname').empty();
			 for(var i=0;i<strJSON.length;i++){
				 if (strJSON[i].institutename == $('#institutename option:selected').text()) {
					 var str="";
					for(var j=0;j<strJSON[i].majorInfos.length;j++){
						str+="<option>"+strJSON[i].majorInfos[j].majorname+"</option>";
					}
					 $('#majorname').html(str);
				 }
				}
	        };
		
    });
}
/*
 	查看按钮事件
 */
function showStuInfo(code) {
	showModel(true);
	var url = "/huaqiao/tclass/getOneClass?code=" + code + "&time=" + new Date().getTime();
	$.getJSON(url, function (strJSON) {
		if (strJSON == null || strJSON.length == 0) {
			alert("找不到数据");
		}
		console.log(JSON.stringify(strJSON));
		$("#myModalLabel").text("班级查看");
		//$("#institutename").val(strJSON.institutename);
		$("#institutename").empty();
		$("#institutename").append("<option value='"+strJSON.institutename+"' selected>"+strJSON.institutename+"</option>");
		//$("#majorname").val(strJSON.majorname);
		$("#majorname").empty();
		$("#majorname").append("<option value='"+strJSON.majorname+"' selected>"+strJSON.majorname+"</option>");
		//$("#teachername").val(strJSON.teachername);
		$("#teachername").empty();
		$("#teachername").append("<option value='"+strJSON.teachername+"' selected>"+strJSON.teachername+"</option>");

		$("#code").val(strJSON.code);
		$("#name").val(strJSON.name);
		$("#grade").val(strJSON.grade);
	});
}
//查看按钮事件
function editStuInfo(code) {
	showModel(false);
	var url = "/huaqiao/tclass/getOneClass?code=" + code + "&time=" + new Date().getTime();
	$.getJSON(url, function (strJSON) {
		if (strJSON == null || strJSON.length == 0) {
			alert("找不到数据");
		}
		console.log(JSON.stringify(strJSON));
		$("#myModalLabel").text("班级编辑");
		//$("#institutename").val(strJSON.institutename);
		$("#institutename").empty();
		$("#institutename").append("<option value='"+strJSON.institutename+"' selected>"+strJSON.institutename+"</option>");
		//$("#majorname").val(strJSON.majorname);
		$("#majorname").empty();
		$("#majorname").append("<option value='"+strJSON.majorname+"' selected>"+strJSON.majorname+"</option>");
		//$("#teachername").val(strJSON.teachername);
		$("#teachername").empty();
		$("#teachername").append("<option value='"+strJSON.teachername+"' selected>"+strJSON.teachername+"</option>");

		$("#code").val(strJSON.code);
		$("#name").val(strJSON.name);
		$("#grade").val(strJSON.grade);
	});
}